<template>
  <div class="registerFrame">
    <el-card class="register-card">
      <template #header>
        <div class="card-header">
          <h2>订餐出餐系统</h2>
          <p>管理端注册</p>
        </div>
      </template>

      <el-form
        :model="registerForm"
        :rules="registerRules"
        ref="registerFormRef"
        @keyup.enter="handleRegister"
      >
        <div>
          <label class="label">账号</label>
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入账号(3-20位字符)"
              prefix-icon="User"
              clearable
            />
          </el-form-item>
        </div>

        <div>
          <label class="label">密码</label>
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              placeholder="请输入密码(6-20位字符)"
              prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>
        </div>

        <div>
          <label class="label">确认密码</label>
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              placeholder="请再次输入密码"
              prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>
        </div>

        <!-- <div>
          <label class="label">安全问题</label>
          <el-form-item prop="securityQuestion">
            <el-select
              v-model="registerForm.securityQuestion"
              placeholder="请选择安全问题"
              style="width: 100%"
            >
              <el-option
                v-for="item in securityQuestions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>

        <div>
          <label class="label">安全问题答案</label>
          <el-form-item prop="securityAnswer">
            <el-input
              v-model="registerForm.securityAnswer"
              placeholder="请输入安全问题答案"
              prefix-icon="ChatDotRound"
              clearable
            />
          </el-form-item>
        </div> -->

        <el-form-item>
          <el-button
            type="primary"
            @click="handleRegister"
            :loading="loading"
            style="width: 100%; margin-top: 20px"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>

      <div class="footer">
        <el-link type="primary" underline="never" @click="goToLogin"
          >已有账号?去登录</el-link
        >
      </div>
    </el-card>
  </div>
</template>

<script lang="js" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import router from '@/router'
import authSerive from '@/api/service/authSerive'
import axios from 'axios'

// 安全问题选项
const securityQuestions = [
  { value: '1', label: '您母亲的姓名是什么？' },
  { value: '2', label: '您父亲的姓名是什么？' },
  { value: '3', label: '您的出生地是哪里？' },
  { value: '4', label: '您的小学校名是什么？' },
  { value: '5', label: '您的第一只宠物的名字是什么？' }
]

// 注册表单数据
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  securityQuestion: '',
  securityAnswer: ''
})

// 表单验证规则
const registerRules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.confirmPassword) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  securityQuestion: [
    { required: true, message: '请选择安全问题', trigger: 'change' }
  ],
  securityAnswer: [
    { required: true, message: '请输入安全问题答案', trigger: 'blur' },
    { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
  ]
})

const loading = ref(false)
const registerFormRef = ref()

// 注册方法
const handleRegister = () => {
  registerFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      const { username, password, confirmPassword} = registerForm;
      console.log(username,password, confirmPassword);
      if(password === confirmPassword){
        const data = {
          username,
          password,
          confirmPassword
        }
        axios.post('http://localhost:8081/admin/register', data).then((res) => {
          console.log(res.data);
          if(res.data && res.data.code === 200){
            ElMessage.success('注册成功')
            loading.value = false
            router.push('/login')
          }
        });
      }
    } else {
      return false
    }
  })
}

// 跳转到登录页
const goToLogin = () => {
  router.push('/admin/login')
}
</script>

<style scoped>
.registerFrame {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  background-image: linear-gradient(120deg, #fdfbfb 0%, #ebedee 100%);
}

.register-card {
  width: 400px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  text-align: center;
}

.card-header h2 {
  margin-bottom: 8px;
  color: #409eff;
}

.card-header p {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.footer {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px 0;
}

.label {
  display: block;
  margin-bottom: 7px;
  font-size: 13px;
}
</style>
